package com.hu.web.servlet;

import com.hu.web.dao.UserDao;
import com.hu.web.dao.impl.UserDaoImpl;
import com.hu.web.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        UserDao userDao = new UserDaoImpl();
        User user = userDao.findUserByUsername(username);
        if (null != user){
            if(password.equals(user.getPassword())){
                request.getRequestDispatcher("success.html").forward(request, response);
            }else {
                request.getRequestDispatcher("error.html").forward(request, response);
            }
        }else {
            request.getRequestDispatcher("error.html").forward(request, response);
        }
    }

}
